<layout name="Common/layout"/>

<link rel="stylesheet" href="__PUBLIC__/Home/style/home.css" type="text/css">
<link rel="stylesheet" href="__PUBLIC__/Home/style/address.css" type="text/css">
<link rel="stylesheet" href="__PUBLIC__/Home/style/order.css" type="text/css">

<script type="text/javascript" src="__PUBLIC__/Home/js/home.js"></script>

<include file="Common/nav"/>
<!-- 页面主体 start -->
<div class="main w1210 bc mt10">
    <div class="crumb w1210">
        <h2><strong>我的订单 </strong><span>> 收货地址</span></h2>
    </div>

<include file="My/menu"/>

    <!-- 右侧内容区域 start -->
    <div class="content fl ml10">
        <div class="address_hd">
            <h3>收货地址薄</h3>

            <?php
                $max_key = max(array_keys($data));
                foreach($data as $k => $v): ?>

            <dl <?php if($max_key == $k): ?> class="last" <?php endif; ?>>
                <dt>
                    <input type="hidden" name="member_id" value="<?php echo session('m_id'); ?>">
                    <?php echo $k+1; ?>
                    <?php echo $v[consignee]; ?>
                    <?php echo $v[province][region_name],$v[city][region_name],$v[district][region_name]; ?>
                    <?php echo $v[address]; ?>
                    <?php echo $v[zipcode]; ?>
                    <?php echo $v[mobile]; ?>
                    <?php if($v['is_default']==1):?><font color="red">[默认]</font><?php endif; ?>
                </dt>
                <dd>
                    <a href="{:U('UserAddress/edit')}?id={$v.id}">修改</a>
                    <a href="<?php echo U('UserAddress/delete',array('id'=>$v['id'])); ?>" onclick="if(!confirm('是否删除该地址?')){return false}">删除</a>
                    <a href="<?php echo U('UserAddress/setDefault',array('id'=>$v['id'])); ?>" onclick="if(!confirm('是否设置为默认地址?')){return false}">设为默认地址</a>
                </dd>
            </dl>
            <?php endforeach; ?>
        </div>

        <div class="address_bd mt10">
            <h4>新增/修改收货地址</h4>
            <form action="<?php if($info):echo U('UserAddress/edit'); else: echo U('UserAddress/add'); endif; ?>" name="address_form" method="post">
                <ul>
                    <li>
                        <label for=""><span>*</span>收 货 人：</label>
                        <input type="text" name="consignee" id="consignee" class="txt" value="<?php echo $info['consignee']; ?>"/>
                    </li>
                    <li>
                        <label for=""><span>*</span>所在地区：</label>
                        <select name="province" id="province">
                            <option value="">请选择</option>
                        </select>
                        <select name="city" id="city">
                            <option value="">请选择</option>
                        </select>
                        <select name="district" id="district">
                            <option value="">请选择</option>
                        </select>
                    </li>
                    <li>
                        <label for=""><span>*</span>详细地址：</label>
                        <input type="text" name="address" id="address" class="txt address" value="<?php echo $info['address']; ?>"/>
                    </li>
                    <li>
                        <label for=""><span>*</span>邮政编码：</label>
                        <input type="text" name="zipcode" id="zipcode" class="txt" style="width:60px" value="<?php echo $info['zipcode']; ?>"/>
                    </li>
                    <li>
                        <label for=""><span>*</span>手机号码：</label>
                        <input type="text" name="mobile" id="mobile" class="txt" value="<?php echo $info['mobile']; ?>"/>
                    </li>
                    <li>
                        <label for="">&nbsp;</label>
                        <input type="checkbox" name="is_default" id="is_default" value="1" <?php if($info['is_default']==1): ?>checked<?php endif; ?> />设为默认地址
                    </li>
                    <li>
                        <label for="">&nbsp;</label>
                        <input type="hidden" name="id" value="<?php echo $info['id'];?>"/>
                        <input type="submit" name="" class="btn" value="保存" />
                    </li>
                </ul>
            </form>
        </div>

    </div>
    <!-- 右侧内容区域 end -->
</div>
<!-- 页面主体 end-->
<script type="text/javascript">
    //循环出省市地区
    $(function () {
        $.ajax({
            url : '<?php echo U("UserAddress/region"); ?>',
            type : 'get',
            dataType : 'json',
            success : function (data) {
                $.each(data,function (k,v) {
                    $('#province').append("<option value='"+v.region_id+"'>"+v.region_name+"</option>")
                });
            }
        });
        $('#province,#city').change(function () {
            var select = $(this);
            $.ajax({
                url : '<?php echo U("UserAddress/region"); ?>',
                type : 'get',
                data : 'id='+select.val(),
                dataType : 'json',
                success : function (data) {
                    select.nextAll('select').empty().append(new Option('请选择'));
                    $.each(data,function (k,v) {
                        select.next().append(new Option(v.region_name,v.region_id));
                    });
                }
            });
        });
    });

    $(function () {
        <?php if($info): ?>
        var region = {province:"<?php echo $info['province']; ?>",city:"<?php echo $info['city']; ?>", district:"<?php echo $info['district']; ?>"};
        $.each(region,function (region_name,region_id) {
            if(region_name == 'province'){
                parent_region_id = 1;
            } else if(region_name == 'city'){
                parent_region_id = region.province;
            } else if(region_name == 'district'){
                parent_region_id = region.city;
            }
            $.ajax({
                url : '<?php echo U("UserAddress/region"); ?>',
                type : 'get',
                data : 'id='+parent_region_id,
                dataType : 'json',
                success : function (data) {
                    $.each(data,function (k,v) {
                        $('#'+region_name).append(new Option(v.region_name,v.region_id,(region_id==v.region_id ? true : false),(region_id==v.region_id ? true : false)));
                    });
                }
            });
        });
});
        <?php else: ?>
        $.ajax({
            url : '<?php echo U("UserAddress/Region"); ?>',
            type : 'get',
            dataType : 'json',
            success : function (data) {
                $.each(data, function (k, v) {
                    $('#province').append(new Option(v.region_name, v.region_id));
                });

            }

        });

    });
    <?php endif; ?>
</script>

<include file="Common/help"/>

